var that,
  api = new (require('../../utils/api.js')),
  headimg = api.getimgurl(),
  headaudio = api.getaudiourl(),
  app = getApp(),
  util = new (require('../../utils/util.js'));
Page({

  /**
   * 页面的初始数据
   */
  data: {
    start:0,
    pagesize:5,
    list:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    that=this;
    if (app.logininfo) {
      that.getlist()
    } else {
      util.setwatcher(app, this.afterlogin)
    }

  },
  afterlogin: {
    token: res => {
      that.getlist();
    }
  },
  getlist(){
    let { start, pagesize,list } = that.data
    api.collectionmpgetlist({ start, pagesize }, res => {
      if (res.data.issuccess) {
        let newlist = res.data.list.map((x, i) => {
          x['imgsrc'] = headimg + x.thumbpath,
            x['audiosrc'] = headaudio + x.src;
          x.date = util.formatDate(new Date(x.date));

          x.views = x.views >= 10000 ? (x.views / 10000).toFixed(1) + "万" : x.views
          return x;
        })
        let targetlist = list.concat(newlist);
        that.setData({ list: targetlist})
      }
    })
  },
  todetail(e) {
    let {
      item, index
    } = e.currentTarget.dataset || 0, { id, cid } = item,{list}=that.data;
    // list.forEach((x,i))
    // console.log(list.join(","))
    console.log(e, 'e')
    wx.navigateTo({
      url: '../collectiondetail/collectiondetail?id=' + id + '&index=' + index + '&cid=' + cid + '&list=' + list,
    })
  },
  gotoIndex: function(events){
    wx.navigateBack({
      delta: 1,
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    return {
      title: app.logininfo.appletname,
      path: `pages/listen/listen?share=`+app.logininfo.userid,
    }
  }
})